package view.admin.userManage;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXRadioButton;
import com.jfoenix.controls.JFXTextField;
import dao.UserDao;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ToggleGroup;
import pojo.user.UserEntity;
import view.Main;
import controller.UserController;

import java.io.IOException;

/**
 * @author Limohan
 */
public class SetChangeUserController {

    private Main myApp;
    private String id;
    public void setApp(Main myApp){
        this.myApp = myApp;
    }
    public void setId(String id){
        this.id = id;
    }

    public SetChangeUserController(){
    }

    @FXML
    private JFXComboBox<String> power;

    @FXML
    private JFXTextField originalAccount;

    @FXML
    private JFXTextField originalPassword;

    @FXML
    private JFXTextField originalName;

    @FXML
    private JFXTextField originalPhone;

    @FXML
    private JFXTextField originalFactoryName;

    @FXML
    private JFXTextField originalDetail;

    @FXML
    private JFXButton newUser;

    @FXML
    private JFXButton returnUserManage;

    @FXML
    void initialize(){
        power.getItems().add("经销商");
        power.getItems().add("云工厂");
    }

    public void originDisplay() throws IOException {
        UserDao userDao = new UserDao();
        UserEntity user = userDao.findUserById(id);
        originalAccount.setText(user.getAccount());
        originalPassword.setText(user.getPassword());
        originalName.setText(user.getName());
        originalPhone.setText(user.getPhone());
        power.setValue(user.getPower());
        originalFactoryName.setText(user.getFactoryName());
        originalDetail.setText(user.getDetail());

    }



    @FXML
    void confirmNewUser(ActionEvent event) throws Exception {
        UserDao userDao = new UserDao();
        userDao.deleteUser(id);
        UserEntity userEntity = new UserEntity();
        UserController userController = UserController.getInstance();
        userEntity.setAccount(originalAccount.getText());
        userEntity.setPassword(originalPassword.getText());
        userEntity.setName(originalName.getText());
        userEntity.setPhone(originalPhone.getText());
        userEntity.setPower(power.getValue());
        userEntity.setDetail(originalDetail.getText());
        userEntity.setFactoryName(originalFactoryName.getText());
        userEntity.setState("关停");
        String flag = userController.register(userEntity);
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("!");
        alert.setContentText(flag);
        alert.showAndWait();




    }

    @FXML
    void returnUserManage(ActionEvent event) throws Exception {
        myApp.goToAdminUserControllerPage();

    }
}
